import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

import Home from "@/views/Home.vue";
import Category from "@/views/Category.vue";
import Shopcart from "@/views/Shopcart.vue";
import User from "@/views/User.vue";
import Login from "@/views/user/Login.vue";
import Register from "@/views/user/Register.vue";
import GoodsList from "@/views/goods/GoodsList.vue";
import GoodsInfo from "@/views/goods/GoodsInfo.vue";
import AddressList from "@/views/user/AddressList.vue";
import AddressEdit from "@/views/user/AddressEdit.vue";
import OrderCreate from "@/views/order/OrderCreate.vue";
import OrderList from "@/views/order/OrderList.vue";
import OrderShow from "@/views/order/OrderShow.vue";
import NewsList from "@/views/news/NewsList.vue";
import NewsInfo from "@/views/news/NewsInfo.vue";
import PhotoList from "@/views/photo/PhotoList.vue";
import PhotoInfo from "@/views/photo/PhotoInfo.vue";

import Working from "@/views/Working.vue";


const routes = [
  { path: "/", redirect: "/home", meta: { title: "首页" } },
  { path: "/home", component: Home, name: "home", meta: { title: "首页" , keepAlive: true} },
  {
    path: "/category",
    component: Category,
    name: "category",
    meta: { title: "分类" ,keepAlive: true},
  },
  {
    path: "/shopcart",
    component: Shopcart,
    name: "shopcart",
    meta: { title: "购物车" ,requireAuth: true},
  },
  { path: "/user", component: User, name: "user", meta: { title: "我的" } },
  {
    path: "/user/login",
    component: Login,
    name: "login",
    meta: { title: "登录" },
  },
  {
    path: "/user/register",
    component: Register,
    name: "register",
    meta: { title: "注册" },
  },
  {
    path: "/goodslist/:category_id",
    component: GoodsList,
    props: true,
    name: "goods_list",
    meta: { title: "商品列表" },
  },
  {
    path: "/goodsinfo/:id",
    component: GoodsInfo,
    props: true,
    name: "goods_info",
    meta: { title: "商品信息" },
  },
  {
    path: "/user/address/list",
    component: AddressList,
    name: "address_list",
    meta: { title: "收货地址",requireAuth: true },
  },
  {
    path: "/user/address/add",
    component: AddressEdit,
    name: "address_add",
    meta: { title: "新增收货地址" ,requireAuth: true},
  },
  {
    path: "/user/address/edit/:id",
    component: AddressEdit,
    props: true,
    name: "address_edit",
    meta: { title: "编辑收货地址",requireAuth: true },
  },
  {
    path: "/order/create",
    component: OrderCreate,
    name: "order_create",
    meta: { title: "下订单",requireAuth: true },
  },
  {
    path: "/user/address/select",
    component: AddressList,
    name: "address_select",
    meta: { title: "选择收货地址" ,requireAuth: true},
  },
  {
    path: "/order/list",
    component: OrderList,
    name: "order_list",
    meta: { title: "我的订单" ,requireAuth: true},
  },
  {
    path: "/order/show/:id",
    component: OrderShow,
    props: true,
    name: "order_show",
    meta: { title: "查看订单" ,requireAuth: true},
  },
  {
    path: "/news/list",
    component: NewsList,
    name: "news_list",
    meta: { title: "新闻资讯列表" },
  },
  {
    path: "/news/show/:id",
    component: NewsInfo,
    props: true,
    name: "news_show",
    meta: { title: "查看新闻资讯" },
  },
  {
    path: "/photo/list",
    component: PhotoList,
    name: "photo_list",
    meta: { title: "图片分享列表" },
  },
  {
    path: "/photo/show/:id",
    component: PhotoInfo,
    props: true,
    name: "photo_show",
    meta: { title: "查看图片" },
  },  
  {
    path: "/working",
    component: Working,   
    name: "working",
    meta: { title: "努力中...." },
  }, 
];
var router = new VueRouter({
  // 解决路由跳转页面没有置顶
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return {
        x: 0,
        y: 0,
      };
    }
  },
  routes,
  linkActiveClass: "mui-active",
});

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  
  if(to.meta.requireAuth) {
    if(!localStorage.getItem('Authorization')) {
        next({name:'login'});       
    }else{
      next()
    }
  }else{  
    next();
  }
});

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

export default router;
